import factoryProtocol.Packet;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Main {
    private static UDPServer udpServer;
    private static int DUP_PORT = 7002;
    private static HttpClient httpClient;
    private static MysqlTool mysqlTool;
    public static byte[]  imei = {0x11,0x22,0x33,0x44,0x55,0x66};
    public static void main(String[] args) throws IOException {

        System.out.println("start pick...\r\n");
        mysqlTool = new MysqlTool();
        httpClient = new HttpClient();
        udpServer = new UDPServer(DUP_PORT) {
            @Override
            protected void onDataReceive(Packet data) {
                mysqlTool.queryAll();

                float temperature;
                float humidity;
                float negaticeO2Ion;
                float pm25;
                float pm10;
                float db;
                String HTTP_URL = "https://chtech.ncjti.edu.cn/bigdata-api/api/environment/recordUpload";

                String code = String.format("%02X%02X%02X%02X%02X%02X",data.imei[0],data.imei[1],data.imei[2],data.imei[3],data.imei[4],data.imei[5]);
                String name = mysqlTool.queryNameByCade(code);
                if(name=="")    // 没有记录IMEI点位
                {
                    System.out.println("----There is no record："+code);
                }else{
                    String location = mysqlTool.queryLocationByCade(code);
                    System.out.println(code+"  "+name+"  "+location);
                    ByteBuffer  buff= data.payload.getData();

                    temperature=  ((0x000000FF & (int)buff.get(0))<<24 | (0x000000FF & (int)buff.get(1))<<16 | (0x000000FF & (int)buff.get(2))<<8 | (0x000000FF & (int)buff.get(3)))/10.0f;
                    humidity=  ((0x000000FF & (int)buff.get(4))<<24 | (0x000000FF & (int)buff.get(5))<<16 | (0x000000FF & (int)buff.get(6))<<8 | (0x000000FF & (int)buff.get(7)))/10.0f;
                    db=   ((0x000000FF & (int)buff.get(8))<<24 | (0x000000FF & (int)buff.get(9))<<16 | (0x000000FF & (int)buff.get(10))<<8 | (0x000000FF & (int)buff.get(11)))/10.0f;
                    pm25=  ((0x000000FF & (int)buff.get(12))<<24 | (0x000000FF & (int)buff.get(13))<<16 | (0x000000FF & (int)buff.get(14))<<8 | (0x000000FF & (int)buff.get(15)));
                    pm10=  ((0x000000FF & (int)buff.get(16))<<24 | (0x000000FF & (int)buff.get(17))<<16 | (0x000000FF & (int)buff.get(18))<<8 | (0x000000FF & (int)buff.get(19)));
                    negaticeO2Ion=  ((0x000000FF & (int)buff.get(20))<<24 | (0x000000FF & (int)buff.get(21))<<16 | (0x000000FF & (int)buff.get(22))<<8 | (0x000000FF & (int)buff.get(23)));

                    String message = httpClient.doPost(HTTP_URL,upLoadData(code,name,location,temperature, humidity, negaticeO2Ion, pm25, pm10, db));
                    System.out.println(message);
                }
            }
        };
        udpServer.start();
    }

    /**
     * 封装成http发送的json参数
     * @param code
     * @param name
     * @param temperature
     * @param humidity
     * @param negaticeo2ion
     * @param pm25
     * @param pm10
     * @param db
     * @return
     */
    private static String upLoadData(String code, String name,  String location,float temperature, float humidity, float negaticeo2ion, float pm25, float pm10, float db) {
        Date dNow = new Date( );
        SimpleDateFormat ft = new SimpleDateFormat ("yyyy-MM-dd HH:mm:ss");
        String dataStr = "{\"pointCode\":\""+code+"\",\"pointName\":\""+name+"\",\"pointLocation\":\""+location+"\",\"temperature\":"+String.format("%.1f",temperature)+",\"humidity\":"+String.format("%.1f",humidity)+",\"negaticeo2ion\":"+String.format("%.1f",negaticeo2ion)+",\"pm25\":"+String.format("%.1f",pm25)+",\"pm10\":"+String.format("%.1f",pm10)+",\"db\":"+String.format("%.1f",db)+",\"updateTime\":\""+ft.format(dNow)+"\"}";
        System.out.println(dataStr);
        return dataStr;
    }


}
